package com.jinzhi.jinzhidata.util;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName DateUtil
 * @Description //TODO 日期工具
 * @Author haoxx
 * @Date 2019/4/24 - 16:42
 * @Version 1.0
 */
public class DateUtil {

    /**
     * @return java.lang.String
     * @author haoxx
     * @Description //TODO 日期对象转字符串
     * @Date 2019/4/24 - 16:43
     * @Param [date, format]
     */
    public static String formatDate(Date date, String format) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        if (date != null) {
            result = sdf.format(date);
        }
        return result;
    }

    /**
     * @return java.util.Date
     * @author haoxx
     * @Description //TODO 符串转日期对象
     * @Date 2019/4/24 - 16:43
     * @Param [str, format]
     */
    public static Date formatString(String str, String format) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
    }

    public static String getCurrentDateStr() {
        return formatDate(new Date(), "yyyyMMddhhmmss");
    }

    public static String getYMdStr() {
        return formatDate(new Date(), "yyyy-MM-dd");
    }
}
